{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from caffe2.python import (\n",
    "    core,\n",
    ")\n",
    "\n",
    "from caffe2.python.fb.dper.layer_models.models import sparse_nn\n",
    "from fblearner.flow.projects.dper.preprocs.ads import build_preproc\n",
    "from fblearner.flow.projects.dper.preprocs.ads_feature_processor import (\n",
    "    ads_feature_processor,\n",
    ")\n",
    "from hiveio import par_init  # noqa\n",
    "import fblearner.flow.projects.dper.flow_types as T\n",
    "import fblearner.flow.projects.dper.utils.assemble as assemble_utils\n",
    "import fblearner.flow.projects.dper.utils.data as data_utils\n",
    "import fblearner.flow.projects.dper.utils.visualize as vis_utils\n",
    "import fblearner.flow.projects.dper.workflows.ads_config as default_config\n",
    "\n",
    "import fblearner.flow.projects.dper.ifbpy.compute_meta as compute_meta\n",
    "from fblearner.flow.projects.dper.ifbpy.execution import test_model_locally\n",
    "import fblearner.flow.projects.dper.utils.visualize as vis_utils\n",
    "import fblearner.flow.projects.dper.utils.perf_estimator_execution as perf_estimator_execution\n",
    "\n",
    "import json\n",
    "core.GlobalInit(['ifbpy'])\n",
    "from IPython.core.debugger import Pdb; \n",
    "ipdb=Pdb()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# when testing a particular flow, load model options from json file, and pass it to model_options\n",
    "# local_prod_jason_file=\"/home/dongli/fbsource/fbcode/caffe2/caffe2/net_config/33252482/prod_model.json\"\n",
    "# with open(local_prod_jason_file, 'r') as f:\n",
    "#     prod_model_options = sparse_nn.MODEL_OPTIONS.decode(json.loads(f.read()))\n",
    "# print(prod_model_options)        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "preproc_options = default_config.DEFAULT_PREPROC_OPTIONS\n",
    "\n",
    "# when testing a particular flow, load model options from json file\n",
    "# load model preproc options from json file\n",
    "# from fblearner.flow.projects.dper.preprocs.ads import build_preproc\n",
    "# local_prod_preproc_jason_file=\"/home/dongli/fbsource/fbcode/caffe2/caffe2/net_config/33252482/prod_preproc.json\"\n",
    "# with open(local_prod_preproc_jason_file, 'r') as f:\n",
    "#     prod_preproc_options = build_preproc.options_flow_type.decode(json.loads(f.read()))\n",
    "# print prod_preproc_options\n",
    "\n",
    "# preproc_options = prod_preproc_options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Finalize config for preprocessor\n",
    "compute_meta.resolve_compute_meta(ads_feature_processor, default_config.DEFAULT_DATASET, preproc_options)\n",
    "print(\"Done: resolve_compute_meta\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Assemble the model given preprocessor and model building fuction\n",
    "model = assemble_utils.assemble_model(\n",
    "        name='sparse_nn',\n",
    "        input_feature_schema=build_preproc.input_feature_schema(\n",
    "            preproc_options),\n",
    "        trainer_extra_schema=build_preproc.trainer_extra_schema(\n",
    "            preproc_options),\n",
    "        build_preproc_fun=build_preproc,\n",
    "        build_model_fun=sparse_nn.build_model,\n",
    "        preproc_options=preproc_options,\n",
    "        model_options= default_config.DEFAULT_MODEL_OPTIONS\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Train model one the given sample dataset\n",
    "estimated_cost = perf_estimator_execution.estimate_perf_locally(model, default_config.DEFAULT_DATASET)\n",
    "print(estimated_cost)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
